package com.tf_cut.admin.service.controller;

import com.alibaba.fastjson.JSON;
import com.tf_cut.admin.annotation.Decrypt;
import com.tf_cut.admin.annotation.Encrypt;
import com.tf_cut.admin.backend.api.BackEndApi;
import com.tf_cut.admin.common.pojo.PageInfo;
import com.tf_cut.admin.common.pojo.Result;
import com.tf_cut.admin.service.vo.ServiceInfoVo;
import com.tf_cut.admin.service.vo.ServicePersonalIncomeTaxSetingVo;
import com.tf_cut.admin.sys.sysuser.service.SysUserService;
import com.tf_cut.admin.sys.sysuser.vo.SysUserVo;
import com.tf_cut.admin.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/service/")
@Slf4j
public class ServiceController {

    @Autowired
    private BackEndApi serviceApi;
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("servicelist")
    public ModelAndView serviceList() {
        return new ModelAndView("service/servicelist", "service", null);
    }

    @GetMapping("serviceinsert")
    public ModelAndView serviceInsert() {
        return new ModelAndView("service/serviceinsert", "service", null);
    }

    @GetMapping("serviceimport")
    public ModelAndView serviceImport() {
        return new ModelAndView("service/serviceimport", "service", null);
    }


    @GetMapping("servicetax")
    public ModelAndView servicetax() {
        return new ModelAndView("service/servicetax", "service", null);
    }
    /**
     * 编辑
     *
     * @param serviceId
     * @return
     */
    @GetMapping("serviceinfo")
    public ModelAndView sellerInfo(String serviceId) {
        ServiceInfoVo avo = null == serviceId ?
                new ServiceInfoVo() :
                JSON.parseObject(serviceApi.getService(serviceId), ServiceInfoVo.class);
        return new ModelAndView("service/serviceinfo", "service", avo);
    }

    /**
     * 删除（D）
     * @param avo
     * @return
     */
    @PostMapping("operation")
    public Result<Boolean> operation(ServiceInfoVo avo) {
        return JSON.parseObject(serviceApi.operationService(avo.getServiceId(),avo.getServiceState()),Result.class);
    }

    /**
     * 新增/修改服务商
     *
     * @param avo
     * @return
     */
    @SuppressWarnings("unchecked")
    @PostMapping("saveService")
    public Result<ServiceInfoVo> saveService(ServiceInfoVo avo) {
        ServiceInfoVo nameV = new ServiceInfoVo();
        nameV.setServiceCom(avo.getServiceCom());
        String nameRes = serviceApi.findListServiceNoPage(JSON.toJSONString(nameV));
        List<ServiceInfoVo> nameList = JSON.parseArray(nameRes,ServiceInfoVo.class);

        ServiceInfoVo loginV = new ServiceInfoVo();
        loginV.setLoginName(avo.getServicePhone());
        String loginRes = serviceApi.findListServiceNoPage(JSON.toJSONString(loginV));
        List<ServiceInfoVo> loginList = JSON.parseArray(loginRes,ServiceInfoVo.class);

        Result<ServiceInfoVo> result = Result.of(avo);
        try {
            //新增系统用户
            SysUserVo sysUserVo = sysUserService.initUser(avo.getServicePhone(),avo.getServicePhone(),"SERVICE_USER").getData();
            avo.setLoginId(sysUserVo.getUserId());
            String rtnJson;
            if (StringUtils.isEmpty(avo.getServiceId())) {
                if(!nameList.isEmpty()){
                    result = Result.of(avo,false,"服务商名称重复");
                }
                if(!loginList.isEmpty()){
                    result = Result.of(avo,false,"登陆名称重复");
                }

                if(!result.isFlag()){
                    return result;
                }
                avo.setServiceState("Y");
                rtnJson = serviceApi.insertServiceInfo(JSON.toJSONString(avo));
                result = JSON.parseObject(rtnJson, Result.class);
            } else {
                Result<ServiceInfoVo> result1 = Result.of(avo);
                nameList.forEach(vo -> {
                    if(!vo.getServiceId().equalsIgnoreCase(avo.getServiceId())){
                        result1.setFlag(false);
                        result1.setMsg("服务商名称重复");
                    }
                });
                loginList.forEach(vo -> {
                    if(!vo.getServiceId().equalsIgnoreCase(avo.getServiceId())){
                        result1.setFlag(false);
                        result1.setMsg("登陆名称重复");
                    }
                });
                if(!result1.isFlag()){
                    return result1;
                }
                rtnJson = serviceApi.updateServiceInfo(JSON.toJSONString(avo));
                result = JSON.parseObject(rtnJson, Result.class);
            }
        } catch (Exception e) {
            log.error("saveService error:{}", e.getMessage(), e);
            avo.setServiceId(null);
            result = Result.of(avo);
            result.setFlag(false);
        }
        return result;
    }

    @PostMapping("list/page")
    public Result<PageInfo<ServiceInfoVo>> page(ServiceInfoVo avo) {
        avo.setServiceState("Y");
        String json = JSON.toJSONString(avo);
        json = serviceApi.findListService(json);
        return Result.of(JSON.parseObject(json, PageInfo.class));
    }
    @Decrypt
    @Encrypt
    @PostMapping("servicelist/list")
    public Result<List<ServiceInfoVo>> list(ServiceInfoVo avo) {
        avo.setServiceState("Y");
        String json = JSON.toJSONString(avo);
        json = serviceApi.findListServiceNoPage(json);
        return Result.of(JSON.parseArray(json, ServiceInfoVo.class));
    }

    @PostMapping("servicelist/spitslist")
    public Result<List<ServicePersonalIncomeTaxSetingVo>> ServicePersonalIncomeTaxSetingVo_list(ServicePersonalIncomeTaxSetingVo avo) {
        String json = JSON.toJSONString(avo);
        json = serviceApi.ServicePersonalIncomeTaxSetingVo_list(json);
        return Result.of(JSON.parseArray(json, ServicePersonalIncomeTaxSetingVo.class));
    }


    @PostMapping("servicelist/spitsave")
    public Result<ServicePersonalIncomeTaxSetingVo> saveService(ServicePersonalIncomeTaxSetingVo avo) {
        Result<ServicePersonalIncomeTaxSetingVo> result;
        try {
            String rtnJson;
            if (StringUtils.isEmpty(avo.getId())) {
                rtnJson = serviceApi.ServicePersonalIncomeTaxSetingInsert(JSON.toJSONString(avo));
                result = JSON.parseObject(rtnJson, Result.class);
            } else {
                rtnJson = serviceApi.ServicePersonalIncomeTaxSetingUpdateInfo(JSON.toJSONString(avo));
                result = JSON.parseObject(rtnJson, Result.class);
            }
        } catch (Exception e) {
            log.error("saveService error:{}", e.getMessage(), e);
            avo.setServiceId(null);
            result = Result.of(avo);
            result.setFlag(false);
        }
        return result;
    }

    @PostMapping("servicelist/spitsdelete")
    public Result<ServicePersonalIncomeTaxSetingVo> spitsdelete(ServicePersonalIncomeTaxSetingVo avo) {
        Result<ServicePersonalIncomeTaxSetingVo> result;
        try {
            String rtnJson;
            rtnJson = serviceApi.ServicePersonalIncomeTaxSetingDelete(JSON.toJSONString(avo));
            result = JSON.parseObject(rtnJson, Result.class);
        } catch (Exception e) {
            log.error("saveService error:{}", e.getMessage(), e);
            avo.setServiceId(null);
            result = Result.of(avo);
            result.setFlag(false);
        }
        return result;
    }
}
